﻿using System;
using System.Globalization;
using System.Web;
using System.Web.Services;
using System.Web.UI.WebControls;
using QuanLyHopDong.Biz;
using QuanLyHopDong.Biz.Model;
using QuanLyHopDong.Web.Common;

namespace QuanLyHopDong.Web.HopDong
{
    public partial class List : System.Web.UI.Page
    {
        private static HopDongBiz _hopDongBiz;
        private static HopDongBiz HopDongManager
        {
            get { return _hopDongBiz ?? (_hopDongBiz = new HopDongBiz()); }
        }

        private EmployeeBiz _employeeBiz;
        private EmployeeBiz EmployeeManager { get { return _employeeBiz ?? (_employeeBiz = new EmployeeBiz()); } }

        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                btnNew.Visible = WebCommon.CurrrentEmployee.IsAdmin;
                btnNew2.Visible = WebCommon.CurrrentEmployee.IsAdmin;
                LoadEmployees();
            }
        }

        private void LoadEmployees()
        {
            if (WebCommon.CurrrentEmployee.IsAdmin)
            {
                var employees = EmployeeManager.GetAllEmployees();
                employees.Insert(0, new Employee { Id = 0, Name = string.Empty });
                ddlEmployees.DataValueField = "Id";
                ddlEmployees.DataTextField = "Name";
                ddlEmployees.DataSource = employees;
                ddlEmployees.DataBind();
            }
            else
            {
                ddlEmployees.Items.Add(new ListItem(WebCommon.CurrrentEmployee.Name, WebCommon.CurrrentEmployee.Id.ToString()));
            }
        }

        [WebMethod(EnableSession = true)]
        public static object HopDongList(int jtStartIndex, int jtPageSize, string jtSorting, string soHopDong, string tenHopDong, string tenDonVi,
            string ngayHoanThienFrom, string ngayHoanThienTo, string ngayQuyetToanFrom, string ngayQuyetToanTo, int trangThai, bool thiCongXong, int employeeId)
        {
            DateTime? dtHoanThienFrom = null, dtHoanThienTo = null, dtQuyetToanFrom = null, dtQuyetToanTo = null;
            // hoan thien
            if (trangThai == 5)
            {
                if (!string.IsNullOrEmpty(ngayHoanThienFrom))
                {
                    DateTime dt;
                    if (DateTime.TryParseExact(ngayHoanThienFrom, Resources.GlobalStrings.DateFormat,
                        CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                    {
                        dtHoanThienFrom = dt;
                    }
                }
                if (!string.IsNullOrEmpty(ngayHoanThienTo))
                {
                    DateTime dt;
                    if (DateTime.TryParseExact(ngayHoanThienTo, Resources.GlobalStrings.DateFormat,
                        CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                    {
                        dtHoanThienTo = dt;
                    }
                }
            }
            if (!string.IsNullOrEmpty(ngayQuyetToanFrom))
            {
                DateTime dt;
                if (DateTime.TryParseExact(ngayQuyetToanFrom, Resources.GlobalStrings.DateFormat,
                    CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                {
                    dtQuyetToanFrom = dt;
                }
            }
            if (!string.IsNullOrEmpty(ngayQuyetToanTo))
            {
                DateTime dt;
                if (DateTime.TryParseExact(ngayQuyetToanTo, Resources.GlobalStrings.DateFormat,
                    CultureInfo.InvariantCulture, DateTimeStyles.None, out dt))
                {
                    dtQuyetToanTo = dt;
                }
            }

            int totalRecords;
            var donVis = HopDongManager.GetHopDongViews(
                jtStartIndex, jtPageSize, jtSorting,
                soHopDong, tenHopDong, tenDonVi,
                dtHoanThienFrom, dtHoanThienTo,
                dtQuyetToanFrom, dtQuyetToanTo,
                trangThai, thiCongXong,
                WebCommon.CurrrentEmployee,
                employeeId,
                out totalRecords);

            return new { Result = "OK", Records = donVis, TotalRecordCount = totalRecords };
        }

        [WebMethod(EnableSession = true)]
        public static object HopDongDelete(int Id)
        {
            if (!WebCommon.CurrrentEmployee.IsAdmin)
                return new { Result = "ERROR", Message = "Không có quyền!" };

            var folderPath = HttpContext.Current.Server.MapPath(string.Format("~/Files/HopDong/{0}", Id));
            HopDongManager.DeleteHopDong(Id, folderPath);

            return new { Result = "OK" };
        }
    }
}